#!/bin/sh
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#
#
# this script will find all core files in a specified
# directory (recusively).  It will get traces and generate
# a mask of that trace to be used to compare core file traces
#

SEARCH_DIR=$1

# single node or cluster?
CLUSTER="N"
if [ -e /opt/hptc/pdsh/nodes ]; then
	CLUSTER="XC"
fi

# if not on an XC cluster are we on a Bright cluster?
if [ $CLUSTER != "XC" ]; then
	if [ -e /cm/README ]; then
		CLUSTER="BRIGHT"
	fi
fi

if [ $CLUSTER == "BRIGHT" ]; then
	echo "Bright Cluster node - $HOSTNAME.  Processing core files..."
fi
if [ $CLUSTER == "XC" ]; then
	echo "XC Cluster node - $HOSTNAME.  Processing core files..."
fi
if [ $CLUSTER == "N" ]; then
	echo "Single node dev system - $HOSTNAME.  Processing core files..."
fi

#
#	get traces from all core files
# 
CORENAME_LIST=`find -L $SEARCH_DIR -name "core.*" 2>/dev/null`
#DIAG_TMP_DIR=$TRAF_VAR/sqdiag_tmp
DIAG_TMP_DIR=$SEARCH_DIR/sqdiag_tmp
mkdir $DIAG_TMP_DIR 2>/dev/null
echo "set output-radix 0x10" > $DIAG_TMP_DIR/gdb_bt_command
echo "bt" >> $DIAG_TMP_DIR/gdb_bt_command
echo "quit" >> $DIAG_TMP_DIR/gdb_bt_command

for CORENAME_FULL in $CORENAME_LIST; do
	echo "   $CORENAME_FULL"

	# get trace from core file
	CORENAME_SHORT=`echo $CORENAME_FULL | sed -e 's@^.*/core.@core.@'`
	CORENAME_BT=$DIAG_TMP_DIR"/bt_"$CORENAME_SHORT
	PROGRAM_NAME=`echo $CORENAME_SHORT | sed -e 's@core.*\.\(.*$\)@\1@'`
	gdb $PROGRAM_NAME $CORENAME_FULL -x $DIAG_TMP_DIR/gdb_bt_command > $CORENAME_BT 2>/dev/null

	#	strip the traces, making a mask so they can be compared
	CORENAME_MASK=$DIAG_TMP_DIR"/mask_"$CORENAME_SHORT
	cat $CORENAME_BT | sed \
		-e '1,/#0 / d' \
		-e 's@0x[0-9a-zA-Z]*@0x????????@g' \
		-e 's@:[0-9][0-9]*@:???@g' \
		-e 's@pv_line=[0-9]*@pv_line=???@g' \
		-e 's@stmtHandle=-[0-9]*@stmtHandle=?????@g' \
		-e 's@stmtHandle=[0-9]*@stmtHandle=?????@g' \
		-e 's@option=[0-9]*@option=?????@g' \
		-e 's@=[0-9]*@=?????@g' \
		-e 's@_reqid=[0-9]*@_reqid=????@g' \
		> $CORENAME_MASK
done

#	if on cluster, copy mask files to common node (which was a parameter)
if [ $CLUSTER != "N" ]; then
	COMMON_NODE=$2
#	echo "pdcp -r -w $COMMON_NODE $DIAG_TMP_DIR $SEARCH_DIR"
	pdcp -r -w $COMMON_NODE $DIAG_TMP_DIR $SEARCH_DIR 2>/dev/null
fi
